package com.example.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSessionEvent;
import jdk.jshell.JShell;
import org.apache.coyote.Request;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpSession;

import javax.naming.Name;
import java.io.File;
import java.io.IOException;
import java.net.CookieStore;
import java.util.*;

@RequestMapping("/request")
@RestController
public class RequestController {


    //单个参数
    @RequestMapping("/login")
    public String login(String name) {
        return "接收参数name：" + name;
    }

//    //基本类型接收参数

//    public Object getInt(int age) {
//        return "接收到参数age:" + age;
//    }
    //传递多个参数
   @RequestMapping("/m1")
    public Object getInt(String name, String password) {
        return "接收到参数name:" + name + "   password:" + password;
    }

    @RequestMapping("/m2")
    //传递对象
    public Object method2(Person p) {
        return p.toString();
    }

    @RequestMapping("/m3")
    //后端参数重命名（后端参数映射）注释
    public Object method3(@RequestParam(value = "time", required = false) String goodtime) {
        return "接收参数good time：" + goodtime;
    }

    @RequestMapping("/m4")
    //传递数组
    public String method4(String[] arry) {
        return Arrays.toString(arry);
    }

    @RequestMapping("/m5")
    //传递集合
    public String method5(@RequestParam List<String> listParam) {
        return "size:" + listParam.size() + ",listParam:" + listParam;
    }

    @RequestMapping("/m6")
    //json对象
    public Object method6(@RequestBody Person person) {
        return person.toString();
    }

    public RequestController() {
    }

    //    @RequestMapping("/m7")
//    //json对象
//    public Object method7(JSONUtils person) {
//        return person.toString();
//    }
    @RequestMapping("/m8/{id}/{name}")
    //获取URL中参数@PathVariable
    public String method8(@PathVariable Integer id, @PathVariable String name) {
        return "解析参数id：" + id + ",name:" + name;
    }

    @RequestMapping("/m9")
    public String method9(@RequestPart("file") MultipartFile file) throws IOException {
        //获取⽂件名称
        String fileName = file.getOriginalFilename();
        //⽂件上传到指定路径
        file.transferTo(new File("D:/temp/" + file.getOriginalFilename()));
        return "接收到⽂件名称为: " + fileName;
    }
    //传统获取cookie
    @RequestMapping("/m10")
    public String method10(HttpServletRequest request, HttpServletResponse response) {


        //获取所有cookie信息
        Cookie[] cookies = request.getCookies();
        //打印Cookie信息
        StringBuilder builder = new StringBuilder();
        if (cookies != null) {
            for (Cookie ck : cookies) {
                builder.append(ck.getName() + ':' + ck.getValue());

            }

        }
        return "Coolie信息： " + builder;
    }
    //简洁获取cookie
    @RequestMapping("/getCookie")
    //该方法通过@CookieValue注解声明了需要获取名为'ljj'的 Cookie，并且这个 Cookie 被设置为必须存在
        public String cookie(@CookieValue("ljj") String ljj){
        return "Coolie信息： " + ljj;
    }
    //Session存储
    @RequestMapping("/setSess")
    public String setsess(HttpServletRequest request){
        HttpSession session=request.getSession();
        if(session !=null){
            session.setAttribute("username","java");
        }
        return "session 存储成功";
    }

    //Session读取
    @RequestMapping("/getSess")
    public String getSess(HttpServletRequest request){
        // 如果 session 不存在, 不会⾃动创建
        HttpSession session =request.getSession(false);
        String username=null;
        if (session!=null && session.getAttribute("username")!=null){
            username = (String) session.getAttribute("username");
        }
        return "username：" + username;
    }
    @RequestMapping("/m11")
    public String method11(HttpServletRequest request,HttpServletResponse response){
        String userAgent=request.getHeader("User-Agent");
        return "userAgent" +":"+userAgent;
    }
    @RequestMapping("/header")
    public String header(@RequestHeader("User-Agent") String userAgent) {
        return "userAgent:" + userAgent;
    }

    @RequestMapping("/index")
    public Object index() {
        return "/index.html";

    }
//    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData(){
        return "该方法返回数据";
    }
    @ResponseBody
    @RequestMapping("returnHtml")
    public String returnHtml(){
        return "<h1>Hello,Html</h1>";
    }
//    @RequestMapping("/index2")
//    public Object index2(){
//        return "/a.js";
//    }
//    @RequestMapping("/index3")
//    public Object index3(){
//        return "/b.css";
//    }
    @RequestMapping("/returnJson")
    @ResponseBody
    public HashMap<String,String>returnJson(){
        HashMap<String,String> map=new HashMap<>();
        map.put("name","lisi");
        map.put("age","22");
        return map;
    }
    @RequestMapping("/setStatus")
    @ResponseBody
    public String setStatus(HttpServletResponse response){
        response.setStatus(401);
        return "状态码设置成功";
    }
    @RequestMapping(value = "/returnJson2",produces ="application/json;charset=utf-8")
    @ResponseBody
    public String returnJson2(){
        return "{\"value\":1}";
    }
    @RequestMapping(value = "/setHeader")
    @ResponseBody
    public String setHeader(HttpServletResponse response){
        response.setHeader("MyHeader","MyHeaderValue");
        return "设置Header成功";
    }
    }

